package com.hss.binderpool;

import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.hss.binderpool.remote.BinderPool;

public class MainActivity extends AppCompatActivity {
    private ISpeak mSpeak;
    private ICalculate mCalculate;
    private int pid = android.os.Process.myPid();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new Runnable() {
            @Override
            public void run() {
                startWork();
            }
        }).start();
    }

    private void startWork() {
        Log.d("cylog","当前进程ID为："+pid);
        Log.d("cylog","获取BinderPool对象............");
        BinderPool binderPool = BinderPool.getInstance(MainActivity.this);     // 1
        Log.d("cylog","获取speakBinder对象...........");
        IBinder speakBinder = binderPool.queryBinder(0);  // 2
        Log.d("cylog","获取speak的代理对象............");
        mSpeak = (ISpeak) ISpeak.Stub.asInterface(speakBinder);    // 3
        try {
            mSpeak.speak();     // 4
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        Log.d("cylog","获取calculateBinder对象...........");
        IBinder calculateBinder = binderPool.queryBinder(1);
        Log.d("cylog","获取calculate的代理对象............");
        mCalculate = (ICalculate) ICalculate.Stub.asInterface(calculateBinder);
        try {
            Log.d("cylog",""+mCalculate.add(5,6));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


}
